#!/bin/sh /etc/rc.common
# Copyright (C) 2014 - 2018 OpenWrt.org

START=99

NAME=kamailio
COMMAND=/usr/sbin/$NAME

RUNDIR=/var/run/$NAME
PIDFILE=$RUNDIR/$NAME.pid

LOG_ERR="/usr/bin/logger -p user.err -s -t $NAME"

USE_PROCD=1

#PROCD_DEBUG=1

check_listen() {
	local value="$1"
	local type="$2"

	local proto host port address result

	[ -z "$value" ] && {
		$LOG_ERR empty $type entry
		return 1
	}

	# [IPv6] - pass through as-is
	case "$value" in
		*\[*\]*)
			options="$options -l $value"
			return
			;;
	esac

	# Count colons. More than 2 means malformed
	# Format: proto:host:port
	if [ "$(echo "$value" | awk -F: '{print NF-1}')" -gt 2 ]; then
		$LOG_ERR init script does not understand $type entry \""$value"\"
		return 1
	fi

	# Parse proto (if present)
	case "$value" in
		udp:*|tcp:*|tls:*|sctp:*)
			proto="${value%%:*}"
			value="${value#*:}"
			;;
	esac

	# Parse port (if present)
	case "$value" in
		*:* )
			host="${value%%:*}"
			port="${value#*:}"
			;;
		*)
			host="$value"
			port=""
			;;
	esac

	# Resolve host to IP or interface
	if [ "$type" = "listen" ]; then
		network_get_ipaddr address "$host" || address="$host"
	else
		network_get_ipaddr6 address "$host" && address="[$address]" || address="$host"
	fi

	# Reconstruct result
	if [ -n "$proto" ]; then
		if [ -n "$port" ]; then
			result="$proto:$address:$port"
		else
			result="$proto:$address"
		fi
	else
		if [ -n "$port" ]; then
			result="$address:$port"
		else
			result="$address"
		fi
	fi

	options="$options -l $result"
}

start_service() {
	local enabled
	local user
	local group
	local shm_memory
	local pkg_memory
	local cfg_file
	local options

	config_load $NAME

	config_get_bool enabled general enabled 0

	if [ $enabled -eq 0 ]; then
		$LOG_ERR service not enabled in /etc/config/$NAME
		return 1
	fi

	config_get user        general user       $NAME
	config_get group       general group      $NAME
	config_get shm_memory  general shm_memory 8
	config_get pkg_memory  general pkg_memory 3
	config_get cfg_file    general cfg_file   /etc/$NAME/$NAME.cfg
	config_get options     general options
	config_get_bool stderr general stderr 0
	config_get debug_level general debug_level 0

	. /lib/functions/network.sh

	config_list_foreach general listen  check_listen listen
	config_list_foreach general listen6 check_listen listen6

	if [ ! -d "$RUNDIR" ]; then
		mkdir -p "$RUNDIR"
		chown "$user":"$group" "$RUNDIR"
	fi

	if [ -d "/etc/kamailio" ]; then
		chown "$user":"$group" /etc/kamailio/ -R
	fi

	procd_open_instance
	procd_set_param command $COMMAND
	procd_append_param command \
		-P $PIDFILE \
		-f "$cfg_file" \
		-m "$shm_memory" \
		-M "$pkg_memory" \
		$options \
		-u "$user" \
		-g "$group" \
		-DDD
	# If log_stderror=no (default) global parameter and -E is not provided, then it writes to syslog daemon
	[ "$stderr" -eq '1' ] && procd_append_param command -E
	procd_append_param command --debug $debug_level

	# silence stderr (useful only for debug)
	procd_set_param stderr 0
	# forward stdout to logd
	procd_set_param stdout 1
	procd_close_instance
}

